import config from '../../mars/conf/config'

(function(exports) {
  //更新基本信息
  var apis = {
    login: {
      url: "/apis/login",
      method: "GET",
    },
    getBaseInfo: {
      method: "GET",
      url: "/api/baseInfo",
    },
    postBaseInfo: {
      method: "POST",
      url: "/api/baseInfo",
    },
    postMenu: {
      method: "POST",
      url: "/api/new"
    },
    getRecord: {
      method: "GET",
      url: "/api/sugar_record",
    },
    // calorie release
    getFoodByType: {
      method: "GET",
      url: "/apis/user/menu/favor",
    },
    checkIsNew: {
      method: "POST",
      url: "/apis/user/check",
    },
    savePunchImgInfo: {
      method: "POST",
      url: "/apis/user/punch/img",
    },
    savePunchInfo: {
      method: "POST",
      url: "/apis/user/punch/text",
    },
    getPunchRecords: {
      method: "GET",
      url: "/apis/user/punch",
    },
    getPunchRecordById: {
      method: "GET",
      url: "/apis/punch",
    },
    getPunchStatistic: {
      method: "GET",
      url: "/apis/punch_statistic",
    },
    getCommunityArticles: {
      method: "GET",
      url: "/apis/punch/everyone"
    },
    bindToUser: {
      method: "GET",
      url: "/apis/user/:userId/bind",
      placeholder: [":userId"]
    },
    getPunchTimeByUserId: {
      method: "GET",
      url: "/apis/user/:userId/punch_times",
      placeholder: [":userId"]
    },
    getPunchRecordByDay: {
      method: "GET",
      url: "/apis/user/:userId/punchs",
      placeholder: [":userId"]
    },
    checkUserIsAdmin: {
      method: "GET",
      url: "/apis/user/:userId/administrator",
      placeholder: [':userId']
    },

    // 2019-03-23 tupperware activity group
    getUserEventByUserId: {
      method: "GET",
      url: "/apis/user/:userId/events",
      placeholder: [":userId"]
    },
    partInGroup:{
      method:"POST",
      url:"/apis/user/:userId/group/:groupId",
      placeholder:[":userId", ":groupId"]
    },
    getEventInfoByEventId: {
      method: "GET",
      url: "/apis/events/:eventId",
      placeholder: [":eventId"]
    },
    getAllAvailbleEvents: {
      method: "GET",
      url: "/apis/events",
    },
    partInEvent: {
      method: "POST",
      url: "/apis/user/:userId/event/:eventId",
      placeholder: [":userId", ":eventId"]
    },
    getMenuListByEventId: {
      method: "GET",
      url: "/apis/user/menu",
    },
    likeThisPunch:{
      method:"POST",
      url:"/apis/punch/:punchId",
      placeholder:[":punchId"]
    },
    getGroupByUserId:{
      method:"GET",
      url:"/apis/groups/user/:userId",
      placeholder:[":userId"]
    },
    getGroupId:{
      method:"GET",
      url:"/apis/groups/:groupId",
      placeholder:[":groupId"]
    },
    saveGroup:{
      method:"POST",
      url:"/apis/groups",
    },
    getUsersUnpunched:{
      method:"GET",
      url:"/apis/groups/users/unpunched"
    },
    getRecentPunchInfo:{
      method:"GET",
      url:"/apis/punch/recently"
    },
    getActivityFirstPunch:{
      method:"GET",
      url:"/apis/punch/activity/first"
    },
    getActivittFinishPunch:{
      method:"GET",
      url:"/apis/punch/activity/finish"
    }, 
    getActivityLastPunch: {
      method: "GET",
      url: "/apis/punch/activity/last"
    },
    getTeammatesPunchedList:{
      method:"GET",
      url:"/apis/punch/teammates"
    }
  }
  for (var key in apis) {
    exports[key] = {
      header: {
        'content-type': 'application/json'
      },
      url: config.domain + apis[key].url,
      method: apis[key].method,
      placeholder: apis[key].placeholder,
    }
  }
  //执行微信请求
  exports.WxRequest = function(params, data, callback) {
    if (params.placeholder) {
      params.placeholder.forEach(item => {
        params.url = params.url.replace(item, data[item.slice('1')]);
      });
    }

    wx.request({
      url: params.url,
      header: params.header,
      method: params.method == "POST" ? "POST" : "GET",
      data: Object.assign({}, data),
      success: function(res) {
        if (res.statusCode == 200) {
          callback(res.data)
        } else {
          throw new Error('Request "' + params.url + '" failed');
        }
      },
      error:function(res){
        console.error(res);
      }
    })
  }

}(module.exports))